﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class ChiTietThucDonDAO : DataProvider
    {
        public void ThemThucdon(ChiTietThucDonDTO ctma)
        {
            connect();
            string sql = "insert into ChiTietThucDon(thuc_don_id,monan_id,so_phan_an) values(" + ctma.THUCDONID + "," + ctma.MONANID + "," + ctma.SOLUONGPHANAN + ")";
            executeNonQuery(sql);
            disconnect();
        }

        public List<ChiTietThucDonDTO> GetChiTietThucdon(int thucdon)
        {
            connect();
            List<ChiTietThucDonDTO> lis = new List<ChiTietThucDonDTO>();
            string sql = "select * from ChiTietThucDon where thuc_don_id=" + thucdon;
            SqlCommand cmd = new SqlCommand(sql, connection);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ChiTietThucDonDTO dto = new ChiTietThucDonDTO();
                dto.THUCDONID = reader.GetInt32(0);
                dto.MONANID = reader.GetInt32(1);
                dto.SOLUONGPHANAN = reader.GetInt32(2);
                lis.Add(dto);
            }
            disconnect();
            return lis;
        }
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ChiTietThucDonDTO dto = new ChiTietThucDonDTO();
            dto.THUCDONID = int.Parse(dt.Rows[i][0].ToString());
            dto.MONANID = int.Parse(dt.Rows[i][1].ToString());
            dto.SOLUONGPHANAN = int.Parse(dt.Rows[i][2].ToString());
            return (object)dto;
        }

        public void CapNhatChiTietThucDon(ChiTietThucDonDTO dto)
        {
            connect();
            string sql = "Update ChiTietThucDon set monan_id=" + dto.MONANID + ", so_phan_an=" + dto.SOLUONGPHANAN + " Where thuc_don_id=" + dto.THUCDONID + " and monan_id=" + dto.MONANID;
            executeNonQuery(sql);
            disconnect();
        }

        public void XoaChiTietThucDon(int thucdon, int monan)
        {
            connect();
            string deleteCommand = "DELETE FROM ChiTietThucDon WHERE thuc_don_id = " + thucdon + " and monan_id=" + monan;
            executeNonQuery(deleteCommand);
            disconnect();
        }
    }
}
